<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../../js/vue.js"></script>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }

    #app {
        width: 600px;
        margin: 30px auto;
    }

    .table {
        width: 600px;
    }

    .item {
        float: left;
        width: 100px;
        height: 40px;
        text-align: center;
        background-color: black;
        color: chocolate;
        line-height: 40px;
    }

    .item>input {
        width: 30px;
        text-align: center;
    }

    .item button {
        width: 20px;
        height: 20px;
        vertical-align: middle;
    }
</style>

<body>
    <div id="app">
        <myhead @setcheckall="setcheckall" :checkall="checkall"></myhead>
        <mylist :list="list" @dellistbyid="delListById"></mylist>
        <myfooter @delall="delall" :totalprice="totalprice" :totalnum="totalnum"></myfooter>
    </div>
</body>

<template id="myhead">
    <div class="tr">
        <div class="item">
            <label><input type="checkbox" @change="setcheckall" :checked="checkall">全选</label>
        </div>
        <div class="item">商品名称</div>
        <div class="item">商品价格</div>
        <div class="item">商品数量</div>
        <div class="item">小计</div>
        <div class="item">操作</div>
    </div>
</template>

<template id="mylist">
    <div class="table">
        <div class="tr" v-for="item in list" :key="item.id">
            <div class="item">
                <input type="checkbox" v-model="item.checked">
            </div>
            <div class="item">{{item.goodsname}}</div>
            <div class="item">{{item.goodsprice}}</div>
            <div class="item">
                <button @click="calc(item,-1)"><span v-show="item.goodsnum>1">-</span></button>
                <input type="text" v-model="item.goodsnum">
                <button @click="calc(item,1)">+</button>
            </div>
            <div class="item">{{item.goodsprice*item.goodsnum}}</div>
            <div class="item" @click="del(item.id)">删除</div>
        </div>
    </div>
</template>

<template id="myfooter">
    <div class="tr">
        <div class="item" @click="delall">批量删除</div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item">总数{{totalnum}}</div>
        <div class="item">总价{{totalprice}}</div>
        <div class="item"></div>
    </div>
</template>

<script>
    let mylist = {
        props: ["list"],
        template: "#mylist",
        methods: {
            calc(item, n) { //加减
                item.goodsnum = item.goodsnum + n;
            },
            del(id) { //单删
                if (confirm("确定删除？"))
                    this.$emit("dellistbyid", id);
            }
        },

    }

    let myhead = {
        props: ["checkall"],
        template: "#myhead",
        methods: {
            setcheckall() { //全选
                let flag = window.event.target.checked;
                this.$emit("setcheckall", flag);
            }
        }
    }

    let myfooter = {
        props: ["totalprice", "totalnum"],
        template: "#myfooter",
        methods: {
            delall() { //批量删除
                this.$emit("delall");
            }
        }
    }
    new Vue({
        el: "#app",
        data: {
            checkall: false,
            list: [
                {id: 1, goodsname: "lili", goodsprice: 18, goodsnum: 1, checked: false},
                {id: 2, goodsname: "lala", goodsprice: 17, goodsnum: 1, checked: false},
                {id: 3, goodsname: "lulu", goodsprice: 20, goodsnum: 1, checked: false},
                {id: 4, goodsname: "keke", goodsprice: 21, goodsnum: 1, checked: false}
            ]
        },
        computed: {// 计算属性
            totalnum() { //数量合
                let n = 0;
                let numlist = this.list.filter(item => item.checked);
                if (numlist.length) {
                    n = numlist.map(item => item.goodsnum).reduce((a, b) => a + b);
                }
                return n;
            },
            totalprice() { //价格合
                let p = 0;
                let pricelist = this.list.filter(item => item.checked);
                if (pricelist.length) {
                    p = pricelist.map(item => item.goodsprice * item.goodsnum).reduce((a, b) => a + b);
                }
                return p;
            }
        },
        watch: {
            list: {
                deep: true,
                handler() {
                    if (this.list.length) {
                        this.checkall = this.list.every(item => item.checked);
                    } else {
                        this.checkall = false;
                    }

                }
            }
        },
        methods: {
            delListById(id) { //删除方法
                this.list = this.list.filter(item => item.id != id);
            },
            setcheckall(flag) { //全选方法
                this.list.forEach(item => item.checked = flag);
            },
            delall() { //批量删除方法
                if (this.list.length) { //如果有购物车有商品
                    let checkedlist = this.list.filter(item => item.checked == true);// 找到被选中的集合
                    console.log(checkedlist);
                    if (checkedlist.length) { //如果有被选中的
                        if (confirm("确定删除？"))
                            this.list = this.list.filter(item => !item.checked);
                    } else {
                        alert("请选中再删除");
                    }
                } else {
                    alert("您还没有商品加入购物车哦");
                }
            }

        },
        components: {
            myhead,
            mylist,
            myfooter
        }
    })
</script>

</html>